// 
// @Auther: by wujehy
// @Email:wujehy@qq.com
// @Data:2021/2/17.
// @Time:下午2:22.
// 
//

#include "ClientApp.h"
#include "Client.h"

// 全局 变量
std::shared_ptr<Client> client_ptr = nullptr;

std::shared_ptr<Client> getClientPtr()
{
    return client_ptr;
}

void ClientApp_init(Dart_Port callbackPort, int status)
{
    // 触发一个 监听器

    // NOTE wujehy 复用 data 的结构, 解析方式 改变一下 即可


    // 初始化
    if (client_ptr == nullptr)
    {
        callDartCallbackData( getListernerPort(), DartAppListernerType_SDKInitBeforeEvent, LISTER_TYPE_KEYWORD );
        debug_console_log( "客户端未初始化 ... 可以初始化" );
        // 其他初始化操作
        client_ptr = std::make_shared<Client>();
        client_ptr->init_logger( debug_console_log );
        client_ptr->setClientStatus( status );
        callDartCallbackData( callbackPort, 200, "初始化成功" );
        callDartCallbackData( getListernerPort(), DartAppListernerType_SDKInitAfterEvent, LISTER_TYPE_KEYWORD );
    } else
    {
        debug_console_log( "客户端已初始化 ... 不需要重复初始化" );
        callDartCallbackData( callbackPort, 400, "客户端已初始化 ... 不需要重复初始化" );
    }
}

void ClientApp_deinit(Dart_Port callbackPort)
{
    // 其他错误判断
    callDartCallbackData( getListernerPort(), DartAppListernerType_SDKUnInitBeforeEvent, LISTER_TYPE_KEYWORD );


    // TODO 回调错误
    debug_console_log( "反初始化客户端 开始" );
    if (client_ptr == nullptr)
    {
        debug_console_log( "客户端未初始化 不需要反初始化" );
    } else
    {
        client_ptr.reset();
        client_ptr = nullptr;
        debug_console_log( "反初始化成功" );
    }
    callDartCallbackData( callbackPort, 200, "反初始化成功" );
    callDartCallbackData( getListernerPort(), DartAppListernerType_SDKUnInitAfterEvent, LISTER_TYPE_KEYWORD );

}

void ClientApp_getStatus(Dart_Port callbackPort)
{
    // 根据业务场景 , code 非 200 就是false
    if (client_ptr == nullptr)
    {
        callDartCallbackData( callbackPort, 401, "客户端未初始化" );
        return;
    }

    if (client_ptr->isClientStatus())
    {
        callDartCallbackData( callbackPort, 200, "" );
    } else
    {
        callDartCallbackData( callbackPort, 400, "" );
    }

}

void ClientApp_setBaseKey(Dart_Port callbackPort, const char *key, const char *data, int data_len)
{
    debug_console_log( "ClientApp_setBaseKey enter ...  " );
    if (client_ptr == nullptr)
    {
        callDartCallbackData( callbackPort, 401, "客户端未初始化" );
        return;
    }
    // 转化
    std::string save_key = "Base:" + std::string( key );
    std::string save_value = std::string( data, data_len );
    // 异步 操作
    client_ptr->threadPool.appendTask(
            [callbackPort, save_key, save_value]()
            {
                debug_console_log( "ClientApp_setBaseKey task enter ...  " );
                client_ptr->setKey( save_key, save_value );
                // 结束一定要返回
                callDartCallbackData( callbackPort, 200, "" );
                debug_console_log( "ClientApp_setBaseKey task end ...  " );
            } );
    debug_console_log( "ClientApp_setBaseKey end ...  " );
}

void ClientApp_delBaseKey(Dart_Port callbackPort, const char *key)
{
    debug_console_log( "ClientApp_delBaseKey enter ...  " );
    if (client_ptr == nullptr)
    {
        callDartCallbackData( callbackPort, 401, "客户端未初始化" );
        return;
    }
    // 转化
    std::string del_key = "Base:" + std::string( key );
    // 异步 操作
    client_ptr->threadPool.appendTask(
            [callbackPort, del_key]()
            {
                debug_console_log( "ClientApp_delBaseKey task enter ...  " );
                client_ptr->delKey( del_key );
                // 结束一定要返回
                callDartCallbackData( callbackPort, 200, "" );
                debug_console_log( "ClientApp_delBaseKey task end ...  " );
            } );
    debug_console_log( "ClientApp_delBaseKey end ...  " );
}

void ClientApp_getBaseKey(Dart_Port callbackPort, const char *key)
{
    debug_console_log( "ClientApp_getBaseKey enter ...  " );
    if (client_ptr == nullptr)
    {
        callDartCallbackData( callbackPort, 401, "客户端未初始化" );
        return;
    }
    // 转化
    std::string del_key = "Base:" + std::string( key );
    // 异步 操作
    client_ptr->threadPool.appendTask(
            [callbackPort, del_key]()
            {
                debug_console_log( "ClientApp_getBaseKey task enter ...  " );
                std::string getValue;
                int ret = client_ptr->getKey( del_key, getValue );
                // 结束一定要返回
                if (ret == EXIT_SUCCESS)
                {
                    // 数据存在
                    callDartCallbackData( callbackPort, 200, "", getValue );
                } else
                {
                    callDartCallbackData( callbackPort, 404, "没找到数据" );
                }
                debug_console_log( "ClientApp_getBaseKey task end ...  " );
            } );
    debug_console_log( "ClientApp_getBaseKey end ...  " );
}
